package com.sun.reflect;

import org.junit.Test;
import sun.reflect.Reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectAPI {
    @Test
    public void testAPI1() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //加载类 获取Class类型
        Class<?> clazz = Class.forName("com.sun.reflect.bean.Person");

        System.out.println("class.getName => " + clazz.getName());
        //获取实例对象
        Object person = clazz.newInstance();
        System.out.println("person的类型 =》 " + person.getClass());
        //获取所有方法
        /*Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }*/
        //通过class对象获取对象的方法
        Method method = clazz.getMethod("he");
        System.out.println(method.getName());
        //通过method对象执行方法
        Object value = method.invoke(person);
        System.out.println(value);
    }
}
